import json
from src.common import commonUtils
from src.common.logger import getLogger
from flask import Blueprint, Response, request
from src.modules.rag.service import retrieval_service

logger = getLogger()
api = Blueprint("retrieval", __name__)

@api.route("/retrieval/rag", methods=['POST'])
def retrieve_rag():
    form = request.form
    logger.info(f"retrieve_rag form: {form}")
    retrieve_docs = retrieval_service.invoke_retrieval_rag(form)
    return Response(json.dumps({ "code": 200, "data": retrieve_docs }), status = 200, mimetype = "application/json")

@api.route("/retrieval/document", methods=['POST'])
def retrieve_document():
    if 'file' not in request.files:
        return Response("没有上传任何文件", status = 200, mimetype = "application/json")

    file = request.files.get("file")
    content = commonUtils.extract_document_content(file)
    logger.info(f"retrieve_document file content: \n{content}")
    return Response(None, status = 200, mimetype = "application/json")
